package com.howlaisi.devops.message

/**
 * 封装请求方法
 * @param reqType 请求类型 POST GET PUT DELETE
 * @param reqBody 请求体 json格式
 * @return 请求结果
 */
def HttpReq(reqType, reqBody) {
    def wecomServer = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=59d5b5fe-d714-4f00-b7ab-a26583b536d8"

    result = httpRequest httpMode: reqType,
            contentType: "APPLICATION_JSON_UTF8",
            consoleLogResponseBody: true,
            ignoreSslErrors: true,
            requestBody: reqBody,
            url: "${wecomServer}"
    return result
}

/**
 * 发送文本消息
 * @param atMobiles 指定需要at的人的手机号数组
 * @param content 文本消息内容，可以传入变量和换行如 "jenkins构建成功\n 阻断: ${BLOCKER}\n 严重：${CRITICAL}"
 * @return
 */
def SendTextMsg(atMobiles, content) {

    reqBody =
        """
        {
            "msgtype": "text", 
            "text": {
                "content": "$content", 
                "mentioned_mobile_list": ["$atMobiles"]
            }
        }
        """
    response = HttpReq('POST', reqBody)
    println(response)
}

def SendMarkdownMsg(atMobiles, content){
    reqBody =
        """
        {  
            "msgtype": "markdown",
            "markdown": {
                "content": "$content"
                "mentioned_mobile_list": ["$atMobiles"]
            },
        }
        """
    response = HttpReq('POST', reqBody)
    println(response)
}
